//
//  KSLanguageManager.m
//  INMOTION
//
//  Created by Kyle Sun on 8/6/15.
//  Copyright (c) 2015 Inmotion. All rights reserved.
//

#import "KSLanguageManager.h"

static KSLanguageManager *_sharedInstance;

NSString * const kIMTAppLanguageKey = @"appLanguage";
NSString * const kIMTChinesePrefix = @"zh";

NSString * const kIMTSystemLanguagePrefix = @"System";
NSString * const kIMTSimplifiedChinesePrefix = @"zh-Hans";
NSString * const kIMTTraditionnalChinesePrefix = @"zh-Hant";
NSString * const kIMTEnglishPrefix = @"en";
NSString * const kIMTKoreanPrefix = @"ko";
NSString * const kIMTRussianPrefix = @"ru";
NSString * const kIMTSpainPrefix = @"es";
NSString * const kIMTFrenchPrefix = @"fr";
NSString * const kIMTJapanesePrefix = @"ja";
NSString * const kIMTGermanPrefix = @"de";


NSString * const kIMTEnglishLanguageName = @"English";
NSString * const kIMTSimplifiedChineseLanguageName = @"简体中文";
NSString * const kIMTTraditionalChineseLanguageName = @"繁體中文";
NSString * const kIMTKoreanLanguageName = @"한국의";
NSString * const kIMTRussianLanguageName = @"русский";
NSString * const kIMTSpainLanguageName = @"Español";
NSString * const kIMTFrenchLanguageName = @"Français";
NSString * const kIMTJapaneseLanguageName = @"日本語";
NSString * const kIMTGermanLanguageName = @"Deutsch";

const NSInteger kIMTChangeLanguageCode = 1;

@implementation KSLanguageManager

+ (KSLanguageManager *)sharedInstance {
    @synchronized (self) {
        if (!_sharedInstance) {
            _sharedInstance = [[KSLanguageManager alloc] init];
        }
        return _sharedInstance;
    }
}

- (void)configLanguage {
    NSString *currentLanguage = [self currentLanguage];
    NSLog(@"Current Language: %@", currentLanguage);
}

- (NSString *)currentLanguage {
    NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:kIMTAppLanguageKey];
    if (language && ![self useSystemLanguage]) {
        return language;
    }
//    return [self setToSystemLanguage];
    
    // 如果没有设置语言，语言默认改为跟随系统
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:true forKey:@"useSystemLanguage"];
    [defaults synchronize];
    return [self setToSystemLanguage];
}

- (NSString *)currentLanguageName {
    NSString *systemLanguage = [self currentLanguage];
    if ([systemLanguage hasPrefix:kIMTSimplifiedChinesePrefix]) {
        return kIMTSimplifiedChineseLanguageName;
    } else if ([systemLanguage hasPrefix:kIMTTraditionnalChinesePrefix]) {
        return kIMTTraditionalChineseLanguageName;
    } else if ([systemLanguage hasPrefix:kIMTKoreanPrefix]) {
        return kIMTKoreanLanguageName;
    } else if ([systemLanguage hasPrefix:kIMTRussianPrefix]) {
        return kIMTRussianLanguageName;
    } else if ([systemLanguage hasPrefix:kIMTSpainPrefix]) {    // 西班牙
        return kIMTSpainLanguageName;
    } else if ([systemLanguage hasPrefix:kIMTFrenchPrefix]) {   // 法语
        return kIMTFrenchLanguageName;
    } else if ([systemLanguage hasPrefix:kIMTJapanesePrefix]) { // 日语
        return kIMTJapaneseLanguageName;
    } else if ([systemLanguage hasPrefix:kIMTGermanPrefix]) {   // 德语
        return kIMTGermanLanguageName;
    } else {
        return kIMTEnglishLanguageName;
    }
}

- (NSString *)currentLanguageOriginal {
    NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:kIMTAppLanguageKey];
    return language;
}

- (void)setLanguage:(NSString *)language {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:language forKey:kIMTAppLanguageKey];
    [defaults synchronize];
}

- (void)setLanguageWithLanguageName:(NSString *)languageName {
    if ([languageName isEqualToString:kIMTSimplifiedChineseLanguageName]) {
        [self setLanguage:kIMTSimplifiedChinesePrefix];
    } else if ([languageName isEqualToString:kIMTTraditionalChineseLanguageName]) {
        [self setLanguage:kIMTTraditionnalChinesePrefix];
    } else if ([languageName isEqualToString:kIMTKoreanLanguageName]) {
        [self setLanguage:kIMTKoreanPrefix];
    } else if ([languageName isEqualToString:kIMTRussianLanguageName]) {
        [self setLanguage:kIMTRussianPrefix];
    } else if ([languageName isEqualToString:kIMTSpainLanguageName]) {      // 西班牙
        [self setLanguage:kIMTSpainPrefix];
    } else if ([languageName isEqualToString:kIMTFrenchLanguageName]) {     // 法语
        [self setLanguage:kIMTFrenchPrefix];
    } else if ([languageName isEqualToString:kIMTJapaneseLanguageName]) {   // 法语
        [self setLanguage:kIMTJapanesePrefix];
    } else if ([languageName isEqualToString:kIMTGermanLanguageName]) {     // 德语
        [self setLanguage:kIMTGermanPrefix];
    } else {
        [self setLanguage:kIMTEnglishPrefix];
    }
}

- (NSString *)setToSystemLanguage {
    NSString *systemLanguage = [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] objectAtIndex:0];
    if ([systemLanguage hasPrefix:kIMTSimplifiedChinesePrefix]) {
        [self setLanguage:kIMTSimplifiedChinesePrefix];
        return kIMTSimplifiedChinesePrefix;
    } else if ([systemLanguage hasPrefix:kIMTTraditionnalChinesePrefix]) {
        [self setLanguage:kIMTTraditionnalChinesePrefix];
        return kIMTTraditionnalChinesePrefix;
    } else if ([systemLanguage hasPrefix:kIMTKoreanPrefix]) {
        [self setLanguage:kIMTKoreanPrefix];
        return kIMTKoreanPrefix;
    } else if ([systemLanguage hasPrefix:kIMTRussianPrefix]) {
        [self setLanguage:kIMTRussianPrefix];
        return kIMTRussianPrefix;
    } else if ([systemLanguage hasPrefix:kIMTSpainPrefix]) {    // 西班牙
        [self setLanguage:kIMTSpainPrefix];
        return kIMTSpainPrefix;
    } else if ([systemLanguage hasPrefix:kIMTFrenchPrefix]) {   // 法语
        [self setLanguage:kIMTFrenchPrefix];
        return kIMTFrenchPrefix;
    } else if ([systemLanguage hasPrefix:kIMTJapanesePrefix]) { // 日语
        [self setLanguage:kIMTJapanesePrefix];
        return kIMTJapanesePrefix;
    } else if ([systemLanguage hasPrefix:kIMTGermanPrefix]) {   // 德语
        [self setLanguage:kIMTGermanPrefix];
        return kIMTGermanPrefix;
    } else {
        [self setLanguage:kIMTEnglishPrefix];
        return kIMTEnglishPrefix;
    }
}

- (void)setUseSystemLanguage:(BOOL)use {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:use forKey:@"useSystemLanguage"];
    [defaults synchronize];
    if (use) {
        [self setToSystemLanguage];
    }
}

- (BOOL)useSystemLanguage {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults boolForKey:@"useSystemLanguage"];
}

- (BOOL)isChinese {
    NSString *language = [self currentLanguage];
    if ([language hasPrefix:kIMTChinesePrefix]) {
        return YES;
    }
    return NO;
}

- (BOOL)isChineseSimplified {
    NSString *language = [self currentLanguage];
    if ([language hasPrefix:kIMTSimplifiedChinesePrefix]) {
        return YES;
    }
    
    return NO;
}

- (BOOL)isChineseTraditional {
    NSString *language = [self currentLanguage];
    if ([language hasPrefix:@"zh-HK"] || [language hasPrefix:@"zh-Hant"]) {
        return YES;
    }
    
    return NO;
}

@end
